package com.yunxin.core.application.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import com.yunxin.common.utils.SnowflakeKeyGenerator;
import com.yunxin.core.application.dto.user.UserJointLoginDTO;
import com.yunxin.core.application.service.UserApplicationOpenapiService;
import com.yunxin.core.domain.external.channel.ChannelExternalService;
import com.yunxin.core.domain.external.channel.ChannelPromotionPriceInfo;
import com.yunxin.core.domain.model.user.UserAuth;
import com.yunxin.core.domain.model.user.UserAuthRepository;
import com.yunxin.core.domain.model.user.UserInfo;
import com.yunxin.core.domain.model.user.UserInfoRepository;
//import com.yunxin.core.domain.service.order.impl.OrderStatisticsHandle;
import com.yunxin.core.domain.service.user.UserService;
import com.yunxin.core.utils.infra.common.enums.KeyGeneratorPrefixEnum;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Objects;

/**
 * @author zero°
 * @since 2023-12-14
 */
@Service
@RequiredArgsConstructor
public class UserApplicationOpenapiServiceImpl implements UserApplicationOpenapiService {

    private final UserInfoRepository userInfoRepository;

    private final UserAuthRepository userAuthRepository;

    private final SnowflakeKeyGenerator sequenceGenerator;

    private final UserService userService;

    private final ChannelExternalService channelExternalService;

//    private final OrderStatisticsHandle orderStatisticsHandle;

    /**
     * 主体
     */
    public static final String AUTH_SUBJECT = "joint";

    /**
     * 登录来源
     */
    public static final String SOURCE = "h5";

    @Override
    public Boolean checkPhoneMd5(String phoneMd5) {
        if (StrUtil.isBlank(phoneMd5)) return false;
        //MD5手机号查询库是否存在
        UserInfo userInfo = userInfoRepository.getUserInfoByMd5Phone(phoneMd5);
        return Objects.isNull(userInfo);
    }

//    @Override
//    public UserJointLoginDTO jointLogin(String phone, String channelPromotionCode, String clientIp, String city, String visitorId) {
//        // 用 identifier + auth_subject 做MD5加密
//        String identifierMd5 = SecureUtil.md5(phone + AUTH_SUBJECT);
//        if (Objects.isNull(visitorId)) {
//            visitorId = identifierMd5;
//        }
//        //根据手机号查询用户
//        UserAuth userAuth = userAuthRepository.getUserAuthByIdentifier(phone, AUTH_SUBJECT);
//        if (Objects.isNull(userAuth)) {
//            userAuth = new UserAuth();
//            userAuth.setAuthSubject(AUTH_SUBJECT);
//            userAuth.setIdentifier(phone);
//            userAuth.setAuthType(1);
//            userAuth.setIdentifierMd5(identifierMd5);
//        }
//        //保存用户授权信息user_auth,用户code不为空保存登录信息
//        userService.userAuthLogin(userAuth, visitorId, channelPromotionCode, city, SOURCE);
//        //根据手机号获取用户数据user_info
//        UserInfo oldUserInfo = userInfoRepository.getUserInfoByPhone(phone);
//        UserJointLoginDTO userJointLoginDTO = new UserJointLoginDTO();
//        if (Objects.isNull(oldUserInfo)) {
//            oldUserInfo = new UserInfo();
//            oldUserInfo.setPhone(phone);
//            oldUserInfo.setChannelPromotionCode(channelPromotionCode);
//            oldUserInfo.setCity(city);
//            oldUserInfo.setVisitorId(visitorId);
//            oldUserInfo.setRegisterTime(new Date());
//            //生成用户code
//            oldUserInfo.setUserCode(sequenceGenerator.generateKey(KeyGeneratorPrefixEnum.USER.getPrefix()));
//            userJointLoginDTO.setIsNewUser(Boolean.TRUE);
//        } else {
//            // 重新设置访问者id、城市、推广code 方便用户登录记录按请求的最新的参数记录
//            oldUserInfo.setVisitorId(visitorId);
//            oldUserInfo.setCity(city);
//            oldUserInfo.setChannelPromotionCode(channelPromotionCode);
//            userJointLoginDTO.setIsNewUser(Boolean.FALSE);
//        }
//        //保存用户，用户登录记录，更新用户授权
//        boolean result = userService.userRegisterLogin(oldUserInfo, identifierMd5, SOURCE);
//        //调用渠道推广转化统计-统计系统注册
//        if (result) {
//            //根据推广code查询渠道推广价格信息
//            ChannelPromotionPriceInfo channelPromotionPriceInfo = channelExternalService.getChannelPromotionPriceInfoByPromotionCode(oldUserInfo.getChannelPromotionCode());
//            if (Objects.nonNull(channelPromotionPriceInfo)) {
//                //渠道统计(将信息发送到MQ)
//                orderStatisticsHandle.statisticsChannelPromotionTransform(channelPromotionPriceInfo.getChannelId(), oldUserInfo.getChannelPromotionCode(),
//                        channelPromotionPriceInfo.getChannelPromotionPrice(),null, PromotionStatisticsTypeEnum.REGISTER);
//            }
//        }
//        userJointLoginDTO.setUserCode(oldUserInfo.getUserCode());
//        userJointLoginDTO.setVisitorId(visitorId);
//        userJointLoginDTO.setPhone(phone);
//        return userJointLoginDTO;
//    }
}
